﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Directory.Web
{
	public partial class Admin_CategoryView : System.Web.UI.Page
	{
		protected int categoryId;
		protected string categoryIdKey = "categoryId";
		protected Directory.Model.CategoryInfo cat = Model.CategoryInfo.NullCategory;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (string.Compare(Request.RequestType.ToLower(), "post") == 0)
				{
					//Response.Clear();
					return;
				}

				this.RolePanel1.AllowRoles = WebSettings.GetInstance().RootCategoryAdminRole;
				if (!int.TryParse(Request.QueryString[categoryIdKey], out categoryId))
				{
					Response.End();
					return;
				}
				if (categoryId == WebSettings.GetInstance().RootCategoryId)
				{
					Response.End();
					return;
				}
				cat = CategoryDataProxy.GetCategory(categoryId);
				if (cat == null)
				{
					cat = Model.CategoryInfo.NullCategory;
					ltPath.Text = null;
				}
				else
				{
					foreach (Directory.Model.CategoryInfo c in CategoryDataProxy.GetRecursiveParentsOfCategory(cat.CategoryId))
					{
						if (c.CategoryId == cat.CategoryId)
						{
							break;
						}
						ltPath.Text += string.Format("<a onclick='GotoCategory({0})' href='#'>{1}</a>»", c.CategoryId, c.CategoryName);
					}
				}
				rptChilds.DataSource = CategoryDataProxy.GetChildsOfCategory(categoryId);
				rptChilds.DataBind();

				HttpCookie authCookie = FormsAuthentication.GetAuthCookie(Page.User.Identity.Name, false);

				hplExport.NavigateUrl = string.Format("CategoryExport.aspx?categoryid={0}&{1}={2}", categoryId, authCookie.Name, authCookie.Value);
			}
		}
	}
}